package com.ruoyi.system.controller;

import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.system.mapper.CustomerAppointmentsMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.CustomerAppointments;
import com.ruoyi.system.service.ICustomerAppointmentsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户预约信息Controller
 * 
 * @author LK
 * @date 2024-06-22
 */
@RestController
@RequestMapping("/system/customer_appointments")
public class CustomerAppointmentsController extends BaseController
{
    @Autowired
    private ICustomerAppointmentsService customerAppointmentsService;


    /**
     * 查询客户预约信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer_appointments:list')")
    @GetMapping("/list")
    public TableDataInfo list(CustomerAppointments customerAppointments)
    {
        startPage();
        List<CustomerAppointments> list = customerAppointmentsService.selectCustomerAppointmentsList(customerAppointments);
        return getDataTable(list);
    }

    //查询未取消的客户预约信息列表
    @PreAuthorize("@ss.hasPermi('system:customer_appointments:list')")
    @GetMapping("/no_cancel_list")
    public TableDataInfo no_cancel_list(CustomerAppointments customerAppointments)
    {
//        startPage();
        //没有前端参数，只查询全部未取消的预约信息
        CustomerAppointments newcustomerAppointments = new CustomerAppointments();
        newcustomerAppointments.setIsCanceled("0");
        List<CustomerAppointments> list = customerAppointmentsService.selectCustomerAppointmentsList(newcustomerAppointments);
        return getDataTable(list);
    }


    /**
     * 导出客户预约信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:customer_appointments:export')")
    @Log(title = "客户预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CustomerAppointments customerAppointments)
    {
        List<CustomerAppointments> list = customerAppointmentsService.selectCustomerAppointmentsList(customerAppointments);
        ExcelUtil<CustomerAppointments> util = new ExcelUtil<CustomerAppointments>(CustomerAppointments.class);
        util.exportExcel(response, list, "客户预约信息数据");
    }

    /**
     * 获取客户预约信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer_appointments:query')")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") String customerId)
    {
        return success(customerAppointmentsService.selectCustomerAppointmentsByCustomerId(customerId));
    }

    /**
     * 新增客户预约信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer_appointments:add')")
    @Log(title = "客户预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CustomerAppointments customerAppointments)
    {
        String customerId = customerAppointments.getCustomerId();
        CustomerAppointments appointments = customerAppointmentsService.selectCustomerAppointmentsByCustomerId(customerId);

        if(appointments==null){
            return toAjax(customerAppointmentsService.insertCustomerAppointments(customerAppointments));
        }else {
            if(Objects.equals(appointments.getIsCanceled(), "0")){
                return AjaxResult.error("该用户已有未取消的预约信息,请联系管理员取消!");
            }
            // 重复预约 1  if(Objects.equals(appointments.getIsCanceled(), "1"))     非0即1
            else  {
                // 重复预约，将已取消的直接删除，在添加
                customerAppointmentsService.deleteCustomerAppointmentsByCustomerId(customerId);
                return toAjax(customerAppointmentsService.insertCustomerAppointments(customerAppointments));
            }
        }
        //重复预约 0
    }


    /**
     * 是否确认预约，更改标志   走的修改机制    无非就是CRUD,只要不是连表查询，就不需要改后端
     */
    @PreAuthorize("@ss.hasPermi('system:customer_appointments:edit')")
    @Log(title = "是否确认预约", businessType = BusinessType.INSERT)
    @PostMapping("/make_sure_submit")
    public AjaxResult make_sure_submit(@RequestBody CustomerAppointments customerAppointments)
    {
        return toAjax(customerAppointmentsService.updateCustomerAppointments(customerAppointments));
    }

    /**
     * 修改客户预约信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer_appointments:edit')")
    @Log(title = "客户预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CustomerAppointments customerAppointments)
    {
        return toAjax(customerAppointmentsService.updateCustomerAppointments(customerAppointments));
    }

    /**
     * 删除客户预约信息
     */
    @PreAuthorize("@ss.hasPermi('system:customer_appointments:remove')")
    @Log(title = "客户预约信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable String[] customerIds)
    {
        return toAjax(customerAppointmentsService.deleteCustomerAppointmentsByCustomerIds(customerIds));
    }
}
